#coding: utf-8 

from position import *

class Matrix():
    def __init__(self,x=None,y=None,val=None):
        self.xMax=0
        self.yMax=0
        self.m=[]

        if (x!=None):
            self.xMax=x
            self.yMax=y
            for i in range(x):
                self.m.append([])
                for j in range(y):
                    self.m[i].append(val)
                
                
    def getitem(self,x,y):
        if x>=self.xMax or x<0 or y<0 or y>=self.yMax:
            return False
        else:
            return self.m[x][y]
    
    def setitem(self,x,y,value):
        if value==True:
            value=1
        elif value==False:
            value=0
            
        while x>=self.xMax:
            col=[]
            for yaux in range(self.yMax):
                col.append(0)
            self.m.append(col)
            self.xMax+=1
            
        while y>=self.yMax:
            for xaux in range(self.xMax):
                self.m[xaux].append(0)
            self.yMax+=1
            
        self.m[x][y]=value


    def popcol(self,num):
        if num in range(self.xMax):
            self.m.pop(num)
            self.xMax-=1
    
    def poprow(self,num):
        if num in range(self.yMax):
            for x in range(self.xMax):
                self.m[x].pop(num)
            self.yMax-=1
            
    def __iter__(self):
        for x in range(self.xMax):
            for y in range(self.yMax):
                yield (self.m[x][y], x, y)  
    
    
    def __repr__(self):
        rep=''
        yrange=range(self.yMax)
        yrange.reverse()
        for y in yrange:
            #~ rep+= str(y+1)+' '+str((self.xMax)*'--'+'\n  ')
            for x in range(self.xMax):
                rep+=str(self.m[x][y])+" "
            rep+='\n'
        #~ rep+= str(0)+' '+str((self.xMax)*'--'+'\n')
        return rep


class LMatrix(list):
    def __init__(self,x,y,val):
        self.matrix=[]
        self.x=x
        self.y=y
        
        for i in range(self.x):
            self.append([])
            for j in range(self.y):
                self[i].append(val)
    
    def __repr__(self):
        rep=''
        for y in range(self.y):
            rep+= str((self.x+1)*'-------'+'\n')
            for x in range(self.x):
                rep+=str(self[x][y])+"\t"
            rep+='\n'
        rep+= str((self.x*1+1)*'-------'+'\n')
        return rep

if __name__ == '__main__':
    
    a=Matrix()
    a.setitem(5,5,True)
    print a
    a.setitem(2,2,True)
    print a
    a.setitem(10,0,True)
    print a
